// 金额格式化 千分位
export function formatPrice(price, decimalsLength = 2) {
  if (!price || /^[-]?[0-9,]+([0-9.]+){0,1}$/.test(price.toString()) == false) {
    //传入值为空或者有非法值（数字和逗号之外的值）都返回0
    return 0;
  }
  if (isNaN(price.toString())) {
    //传入值若为千分位金额或者不完整小数则返回原值
    return price;
  }
  let usLoacalPrice = Number(
    Number(price.toString().replace(",", "")).toFixed(decimalsLength)
  ).toLocaleString();
  const nums = usLoacalPrice.split(".");
  if (nums[1]) {
    if (nums[1].length === 1) {
      return usLoacalPrice + "0";
    }
    return usLoacalPrice;
  } else {
    return usLoacalPrice + ".00";
  }
}
// 金额格式化 千分位（可输入）
export function formatPriceEdit(price, decimalsLength = 2) {
  if (!price || /^[-]?[0-9,]+([0-9.]+){0,1}$/.test(price.toString()) == false) {
    //传入值为空或者有非法值（数字和逗号之外的值）都返回0
    return "";
  }
  if (
    isNaN(price.toString()) ||
    price.toString().charAt(price.toString().length - 1) == "."
  ) {
    //传入值若为千分位金额或者不完整小数则返回原值
    return price;
  }
  let priceNum = price.toString().replace(/[,]/g, "");
  let usLoacalPrice = Number(
    Number(priceNum).toFixed(decimalsLength)
  ).toLocaleString();
  const nums = price.toString().split(".");
  if (nums[1] && nums[1].length == 1 && nums[1][0] == "0") {
    return usLoacalPrice + ".0";
  }
  if (nums[1] && nums[1].length == 2 && nums[1] == "00") {
    return usLoacalPrice + ".00";
  }
  if (
    nums[1] &&
    nums[1].length == 2 &&
    nums[1][0] != "0" &&
    nums[1][1] == "0"
  ) {
    return usLoacalPrice + "0";
  }
  return usLoacalPrice;
  // const nums = usLoacalPrice.split('.');
  // if (nums[1]) {
  //     if (nums[1].length === 1) {
  //         return usLoacalPrice + '0';
  //     }
  //     return usLoacalPrice;
  // } else {
  //     return usLoacalPrice + '.00';
  // }
}
//金额 去除千分位格式
export function regainOriginalMoneyFormat(money) {
  var patt = /[^\d\.-]/g;
  var Result = patt.test(money);
  if (Result) {
    return money ? money.replace(/[^\d\.-]/g, "") : 0;
  } else {
    return money;
  }
}
//利率格式化
export function formatRate(rate, days = 1) {
  if (!rate) {
    return 0;
  }
  return numberMul(numberMul(rate, 100), days) + "%";
}
//获取当前月具体日期
export function getSpecifyDate(num) {
  let date = new Date(new Date().toLocaleDateString());
  if (num) {
    date.setDate(num);
  }
  return Date.parse(date);
  // return formatDate(date, 'yyyy-MM-dd');
}
//时间格式化
function formatDate(date, fmt) {
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(
      RegExp.$1,
      (date.getFullYear() + "").substr(4 - RegExp.$1.length)
    );
  }
  let o = {
    "M+": date.getMonth() + 1,
    "d+": date.getDate(),
    "h+": date.getHours(),
    "m+": date.getMinutes(),
    "s+": date.getSeconds()
  };
  for (let k in o) {
    if (new RegExp(`(${k})`).test(fmt)) {
      let str = o[k] + "";
      fmt = fmt.replace(
        RegExp.$1,
        RegExp.$1.length === 1 ? str : padLeftZero(str)
      );
    }
  }
  return fmt;
}

function padLeftZero(str) {
  return ("00" + str).substr(str.length);
}
//计算天数差的函数，通用
export function dateDiff(sDate1, sDate2) {
  if (!sDate1 || !sDate2) {
    return 0;
  }
  let dateSpan, iDays;
  sDate1 = !isNaN(sDate1) ? sDate1 : Date.parse(sDate1);
  sDate2 = !isNaN(sDate2) ? sDate2 : Date.parse(sDate2);
  dateSpan = sDate2 - sDate1;
  dateSpan = Math.abs(dateSpan);
  iDays = Math.floor(dateSpan / (24 * 3600 * 1000));
  return parseInt(iDays) + 1;
}
//校验只能输入中文
export function changeValue(val) {
  var patt = /^[\u4e00-\u9fa5]{0,}$/;
  var Result = patt.test(val);
  return Result;
}
//校验只能输入英文和数字
export function changeValueE(val) {
  var patt = /^[A-Za-z0-9]+$/;
  var Result = patt.test(val);
  return Result;
}
//数字金额 转换 中文大写金额
export function switchCaptainMoney(n) {
  if (!/^(0|[1-9]\d*)(\.\d+)?$/.test(n)) return "数据非法";
  var unit = "千百拾亿千百拾万千百拾元角分",
    str = "";
  n += "00";
  var p = n.indexOf(".");
  if (p >= 0) n = n.substring(0, p) + n.substr(p + 1, 2);
  unit = unit.substr(unit.length - n.length);
  for (var i = 0; i < n.length; i++)
    str += "零壹贰叁肆伍陆柒捌玖".charAt(n.charAt(i)) + unit.charAt(i);
  return str
    .replace(/零(千|百|拾|角)/g, "零")
    .replace(/(零)+/g, "零")
    .replace(/零(万|亿|元)/g, "$1")
    .replace(/(亿)万|壹(拾)/g, "$1$2")
    .replace(/^元零?|零分/g, "")
    .replace(/元$/g, "元整");
}
//浮点数相乘
export function numberMul(arg1, arg2) {
  if (arg1 && arg2) {
    let m = 0;
    let s1 = arg1.toString();
    let s2 = arg2.toString();
    try {
      m += s1.split(".")[1].length;
    } catch (e) {}
    try {
      m += s2.split(".")[1].length;
    } catch (e) {}

    return (
      (Number(s1.replace(".", "")) * Number(s2.replace(".", ""))) /
      Math.pow(10, m)
    );
  } else {
    return 0;
  }
}
//浮点数相减
export function numberSub(arg1, arg2) {
  let re1, re2, m, n;
  try {
    re1 = arg1.toString().split(".")[1].length;
  } catch (e) {
    re1 = 0;
  }
  try {
    re2 = arg2.toString().split(".")[1].length;
  } catch (e) {
    re2 = 0;
  }
  m = Math.pow(10, Math.max(re1, re2));
  n = re1 >= re2 ? re1 : re2;
  return Number(((arg1 * m - arg2 * m) / m).toFixed(n));
}
//浮点数相除
export function numberDiv(arg1, arg2, len = 2) {
  if (arg1 && arg2) {
    let t1 = 0,
      t2 = 0,
      r1,
      r2;
    try {
      t1 = arg1.toString().split(".")[1].length;
    } catch (e) {}
    try {
      t2 = arg2.toString().split(".")[1].length;
    } catch (e) {}
    r1 = Number(arg1.toString().replace(".", ""));
    r2 = Number(arg2.toString().replace(".", ""));
    return Number(((r1 / r2) * Math.pow(10, t2 - t1)).toFixed(len));
  } else {
    return 0;
  }
}
export function maxDate(...dates) {
  return Math.max.apply(null, ...dates);
}
export function minDate(...dates) {
  return Math.min.apply(null, ...dates);
}
